import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { UserI, UserFiltersI } from '@/types/user'

export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref('')
    const userInfo = ref<UserI>({} as UserI)
    const userFilter = ref<UserFiltersI | undefined>(undefined)

    const setToken = (val: string) => {
      token.value = val
    }

    const setUserInfo = (val: UserI) => {
      userInfo.value = val
    }

    const setUserFilter = (val: UserFiltersI) => {
      userFilter.value = val
    }

    const removeToken = () => {
      token.value = ''
    }

    const removeUserInfo = () => {
      userInfo.value = {} as UserI
    }

    const removeUserFilter = () => {
      userFilter.value = undefined
    }

    return {
      token,
      setToken,
      removeToken,
      userInfo,
      setUserInfo,
      removeUserInfo,
      userFilter,
      setUserFilter,
      removeUserFilter,
    }
  },
  {
    persist: {
      storage: sessionStorage,
    },
  },
)
